package com.sevencar.activity;


import com.example.colorpicker.ColorBar;
import com.example.colorpicker.ColorPickerPad;
import com.example.colorpicker.ColorPickerPad.OnColorChangedListener;
import com.example.lightble.CmdSend;
import com.example.lightble.R;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;

public class ColorPadActivity extends Activity{

	 private ColorPickerPad colorPicker;
	 private ColorBar  mColorBar;
	 private Button button;
	 private LinearLayout  mlayout;
	 private CmdSend  mCmdSend;
	 private Button btnResetWhite;
	 
	 
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.color_pad_layout);
		
		  colorPicker = (ColorPickerPad) findViewById(R.id.colorPicker);
		  mColorBar=(ColorBar)findViewById(R.id.id_color_pad_colorbar);
		  mlayout=(LinearLayout)findViewById(R.id.id_main_linearlayout);
		  
//		  	int tmpColor=RGBTwoApplication.getInstance().mCurrentColor;  
		  
		    SharedPreferences sp = getSharedPreferences("color", 0);
	 		
		  	int tmpColor = sp.getInt("currentcolor",Color.RED );
		  	
		  	colorPicker.setColor(tmpColor);  
		  
	        colorPicker.setOnColorChangedListener(new OnColorChangedListener() {
				
				@Override
				public void onColorChanged(int color) {
					 currentColor=color;  
	                 isColorChanged=true;
				}
			});
	        colorPicker.setColorBar(mColorBar);
	        mCmdSend=CmdSend.getInstance();
	        
	        mColorBar.setValue(1);
	        mColorBar.setOnColorChangedListener(new ColorBar.OnColorChangedListener() {
				
				@Override
				public void onColorChanged(int color) {
					// TODO Auto-generated method stub
				//	mlayout.setBackgroundColor(color);
					 currentColor=color;
					 Log.i("sno","A="+Color.alpha(currentColor)+" R="+Color.red(currentColor)+" G="+Color.green(currentColor)+" B="+Color.blue(currentColor));
	                 isColorChanged=true;
				}
			});
	        
	        btnResetWhite=(Button)findViewById(R.id.id_color_pad_restore_white);
	        btnResetWhite.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					currentColor=0xFFFFFF;
					mColorBar.setColor(currentColor);
					colorPicker.setColor(currentColor);
//					mlayout.setBackgroundColor(currentColor);
					
					byte[] test=mCmdSend.getOneKeyWhiteArray();
					HomeFragment.mBLEServiceInstance.sendData(test); 
			    	Toast.makeText(ColorPadActivity.this, "发送一键白光数据包", Toast.LENGTH_SHORT).show();
				}
			});
	}
	 @Override
	    public void onResume() {
	    	// TODO Auto-generated method stub
	    	super.onResume();
	    	startSendThread();
	    }
	    @Override
	    public void onPause() {
	    	// TODO Auto-generated method stub
	    	super.onPause();
	    	stopSendThread();
	    }
	    
	//---------------------------------------发送数据线程-----------
	 private int currentColor;
	 private final int SEND_TIME=80;
	 private boolean isColorChanged=false;
	 private boolean bSendThreadEnable=false;
	 private SendDataThread mSendThread;
	 private void startSendThread()
	 {
		 if(true == bSendThreadEnable)
		{
			 bSendThreadEnable = false;  //断开之前的接收数据线程.
			try 
			{
				Thread.sleep(50);
			}
			catch (InterruptedException e) 
			{
				e.printStackTrace();
			}
		}
		 bSendThreadEnable=true;
		 mSendThread=new SendDataThread(null);
		 mSendThread.start();
	 }
	 private void stopSendThread()
	 {
	 	bSendThreadEnable = false;  //断开之前的接收数据线程.
		try 
		{
			Thread.sleep(50);
		}
		catch (InterruptedException e) 
		{
			e.printStackTrace();
		}
	 }
	 class SendDataThread extends Thread
		{
			final int USB_DATA_BUFFER = 8192;
			
			Handler mHandler;
			Message msg;
			SendDataThread(Handler h) 
			{
				mHandler = h;
				this.setPriority(MAX_PRIORITY);			
			}
			public void run() 
			{
				isColorChanged=false;
				while (true == bSendThreadEnable) 
				{
					try 
					{
						Thread.sleep(SEND_TIME);
					}
					catch (InterruptedException e){
						e.printStackTrace();
					}	
					if(isColorChanged)
					{
						sendColor();
						isColorChanged=false;
					}
				}
			}
		}	
	    public static byte[] int2Byte(int intValue) {  
	        byte[] b = new byte[4];  
	        for (int i = 0; i < 4; i++) {  
	            b[i] = (byte) (intValue >> 8 * (3 - i) & 0xFF);  
	        }  
	        return b;  
	    }
		 private void sendColor()
		 {
			   byte[] test=mCmdSend.getRGBCmdArray(currentColor,currentColor);
	    	    HomeFragment.mBLEServiceInstance.sendData(test); 
		 }
	 
}
